﻿using UnityEngine;
using System.Collections;

public class planet : MonoBehaviour {

	public float rotation_speed = 0.0f;
	public float gravity_force = 0.0f;

	// Use this for initialization
	void Start () {

		gravity_force=Mathf.PI*(transform.localScale.x*transform.localScale.x);
		rigidbody.mass=gravity_force;
	
	}
	
	// Update is called once per frame
	void FixedUpdate () {

		rigidbody.AddTorque(new Vector3(0f,0f,rotation_speed));
		ApplyGravity();
	
	}


	void ApplyGravity()
	{
		GameObject[] gos;
		gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];
		Vector3 position = transform.position;
		
		foreach (GameObject go in gos)
		{
			if(go.rigidbody!=null)
			{
				if(go.rigidbody.isKinematic==false)
				{
					float dist = Vector3.Distance(go.transform.position, position)-(go.transform.localScale.x+transform.localScale.x)/2;

					if(dist>0.01f)
					{
						Vector3 dir = position-go.transform.position;
						float g = gravity_force/(dist*dist);
						
						if(g>0.01f)
							go.rigidbody.AddForce(dir.normalized*g);
					}
				}
			}
		}
	}
}
